package de.uni.bremen.estudent.lbs.util;


import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.CheckBox;

import de.uni.bremen.estudent.lbs.R;
import de.uni.bremen.estudent.lbs.provider.EstudentLBSContract.Categories;

import java.util.HashMap;
import java.util.Map;


/**
 * Adapter for the CategoryListview and the database.
 *
 * @author bahlfeld
 * @version 1.0 28.02.2012
 */
public final class CategoryListAdapter extends SimpleCursorAdapter {

    /**
     * The viewholder pattern.
     *
     * @author bahlfeld
     * @version 1.0 28.02.2012
     */
    public static class ViewHolder {
        public CheckBox categoryCheckBox;
        public int categoryId;
        int categoryIdHolder;
        int categoryNameHolder;
    }

    private final Map<String, Boolean> mCategoryIsEnabled;
    private final SharedPreferences mPrefs;

    public CategoryListAdapter(Context context, int layout, Cursor c,
                               String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        mPrefs = context.getSharedPreferences(Constants.PREFS_NAME, 0);
        mCategoryIsEnabled = new HashMap<String, Boolean>();
        mCategoryIsEnabled.put(Constants.BIBLIOTHEK,
                               mPrefs.getBoolean(Constants.BIBLIOTHEK, true));
        mCategoryIsEnabled.put(Constants.DIENSTLEISTUNGEN, mPrefs
                                        .getBoolean(Constants.DIENSTLEISTUNGEN,
                                                    true));
        mCategoryIsEnabled.put(Constants.GASTRONOMIE,
                               mPrefs.getBoolean(Constants.GASTRONOMIE, true));
        mCategoryIsEnabled.put(Constants.GEBAEUDE,
                               mPrefs.getBoolean(Constants.GEBAEUDE, true));
        mCategoryIsEnabled.put(Constants.KIOSK,
                               mPrefs.getBoolean(Constants.KIOSK, true));
        mCategoryIsEnabled.put(Constants.SPORT,
                               mPrefs.getBoolean(Constants.SPORT, true));
        mCategoryIsEnabled.put(Constants.SCHWIMMEN,
                               mPrefs.getBoolean(Constants.SCHWIMMEN, true));
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

        ViewHolder vh = (ViewHolder) view.getTag();
        if (vh == null) {
            vh = new ViewHolder();
            vh.categoryCheckBox = (CheckBox) view
                                            .findViewById(R.id.categoryCheckBox);
            // TODO Name der CategoryID
            vh.categoryIdHolder = cursor.getColumnIndexOrThrow(Categories.CATEGORY_ID);
            vh.categoryNameHolder = cursor.getColumnIndexOrThrow(Categories.CATEGORY_NAME);
            view.setTag(vh);
        }
        String categoryName = cursor.getString(vh.categoryNameHolder);
        vh.categoryCheckBox.setText(categoryName);
        if (mCategoryIsEnabled != null) {
            vh.categoryCheckBox.setChecked(mCategoryIsEnabled.get(categoryName));
        }
        vh.categoryId = cursor.getInt(vh.categoryIdHolder);
    }
}
